package com.e7.util;

import java.util.Random;

public class Randommer {
	private Random random = new Random();

	public static void main(String[] args) {
		Randommer r = new Randommer();
		System.out.println(r.random(8, 1));
	}

	/**
	 * generate a "random" characters array
	 * 
	 * @param lenght
	 *            length of the "random" characters array
	 * @param type
	 *            <ul>
	 *            <li>1->only letters
	 *            <li>2->only numbers
	 *            <li>3->letters and numbers
	 *            </ul>
	 * @return
	 */
	public char[] random(int length, int type) {
		switch (type) {
		case 1:
			return this.randomLetters(length);
		case 2:
			return this.randomNumbers(length);
		default:
			return this.randomNumters(length);
		}
	}

	private char[] randomNumters(int length) {
		char[] captcha = new char[length];
		for (int i = 0; i < length; i++) {
			if (this.random.nextBoolean()) {
				captcha[i] = (char) ('a' + random.nextInt(26));
			} else {
				captcha[i] = (char) ('0' + random.nextInt(10));
			}
		}
		return captcha;
	}

	private char[] randomNumbers(int length) {
		char[] captcha = new char[length];
		for (int i = 0; i < length; i++) {
			captcha[i] = (char) ('0' + random.nextInt(10));
		}
		return captcha;
	}

	private char[] randomLetters(int length) {
		char[] captcha = new char[length];
		for (int i = 0; i < length; i++) {
			captcha[i] = (char) ('a' + random.nextInt(26));
		}
		return captcha;
	}
}
